package com.sunwayworld.common.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class SequencesUtils {

	public static void main(String[] args) {
		String temp = "";
		temp = getCurrval("KCCKD");
		System.out.println(temp);

	}

	public static String getNextval(String key) {
		Connection conn = null;

		PreparedStatement pstmt = null;
		ResultSet rs = null;

		VSequences seq = new VSequences();

		try {
			conn = ConnManager.getConnection();
			conn.setAutoCommit(false);
			pstmt = conn
					.prepareStatement("update t_sequences t set curval=curval+1 where t.mid = ? ");
			pstmt.setString(1, key);
			pstmt.execute();
			pstmt.close();

			pstmt = conn
					.prepareStatement("select * from t_sequences t where t.mid = ?");

			pstmt.setString(1, key);
			rs = pstmt.executeQuery();
			if (rs.next()) {

				BeanUtils.copyProperties(rs, seq);
			}
			seq = CheckSequences(conn, seq);

			String temp = seq.getExpression();
			temp = temp.replaceAll("yyyy", seq.getYears());
			temp = temp.replaceAll("mm", seq.getMonths());

			int index = temp.indexOf("x");
			int len = temp.length() - index;
			String _temp = getCurrVal(seq.getCurval(), len);
			temp = temp.substring(0, index);
			conn.commit();
			return temp + _temp;
		} catch (Exception ex) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}

			throw new RuntimeException(ex.getMessage());
		} finally {
			ConnManager.close(conn, pstmt, rs);
		}

	}

	public static String getCurrval(String key) {
		Connection conn = null;

		VSequences seq = new VSequences();

		PreparedStatement pstmt = null;
		ResultSet rs = null;

		try {
			conn = ConnManager.getConnection();
			conn.setAutoCommit(false);

			pstmt = conn
					.prepareStatement("select * from t_sequences t where t.mid = ?");

			pstmt.setString(1, key);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				BeanUtils.copyProperties(rs, seq);
			}
			seq = CheckSequences(conn, seq);

			String temp = seq.getExpression();
			temp = temp.replaceAll("yyyy", seq.getYears());
			temp = temp.replaceAll("mm", seq.getMonths());

			int index = temp.indexOf("x");
			int len = temp.length() - index;
			String _temp = getCurrVal(seq.getCurval(), len);
			temp = temp.substring(0, index);
			conn.commit();
			return temp + _temp;
		} catch (Exception ex) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				e.printStackTrace();
			}

			throw new RuntimeException(ex.getMessage());
		} finally {
			ConnManager.close(conn, pstmt, rs);
		}

	}

	private static VSequences CheckSequences(Connection conn, VSequences seq) {
		PreparedStatement pstmt = null;
		try {

			if (seq.getCurval() >= seq.getMaxval()) {
				throw new RuntimeException("主键生成器超出最大值");
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
			String currDate = sdf.format(new Date());
			String nf = currDate.substring(0, 4);
			String yf = currDate.substring(4, 6);

			int nf_d = Integer.parseInt(nf);
			int yf_d = Integer.parseInt(yf);

			int years_d = Integer.parseInt(seq.getYears());
			int months_d = Integer.parseInt(seq.getMonths());

			if (4 != seq.getYears().length()) {
				throw new RuntimeException("年份设置格式不正确(2007)");
			}
			if (2 != seq.getMonths().length()) {
				throw new RuntimeException("年份设置格式不正确(01)");
			}
			if (nf_d < years_d || yf_d < months_d || yf_d > 12 || yf_d < 1) {
				throw new RuntimeException("当前应用程序服务器时间设置错误");
			}

			if (nf_d > years_d) {
				seq.setYears(nf);
				seq.setCurval(seq.getMinval());
				pstmt = conn
						.prepareStatement("update t_sequences t set years= ?,curval=minval  where t.mid = ? ");
				pstmt.setString(1, seq.getYears());
				pstmt.setString(2, seq.getMid());
				pstmt.execute();

			}
			if (yf_d > months_d) {
				seq.setMonths(yf);
				seq.setCurval(seq.getMinval());
				pstmt = conn
						.prepareStatement("update t_sequences t set months= ?,curval=minval  where t.mid = ? ");
				pstmt.setString(1, seq.getMonths());
				pstmt.setString(2, seq.getMid());
				pstmt.execute();
			}

			return seq;

		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException("主键生成错误");
		} finally {
			ConnManager.close(null, pstmt);
		}

	}

	private static String getCurrVal(int curval, int len) {
		String _curval = String.valueOf(curval);
		String _revval = "";
		for (int i = 0, n = len - _curval.length(); i < n; i++) {
			_revval += "0";
		}
		return _revval + _curval;
	}
}
